/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.ui.models;

import bgu.sonar.util.ui.visual.HasVisualData;
import javax.swing.ImageIcon;
import org.openide.util.ImageUtilities;

/**
 *
 * @author bennyl
 */
public class SingleSelectionTabularModel<T> extends AbstractTabularModel<T> {

    public static ImageIcon DEFAULT_CHECK_BOX_ICON = new ImageIcon(ImageUtilities.loadImage("resources/img/check-box.png"));
    T selectedObjects = null;

    public SingleSelectionTabularModel() {
        super("selected?", "Name");
        setColumnEditable(0, Boolean.class);
        setColumnIcon(0, DEFAULT_CHECK_BOX_ICON);
    }

    @Override
    public Object extractValue(T element, String value) {
        switch (value) {
            case "selected?":
                return selectedObjects == element;
            case "Name":
                return HasVisualData.Support.extract(element).getVisualName();
        }
        return "???";
    }

    @Override
    public void importValue(T element, String column, Object value) {
        if (value instanceof Boolean) {
            Boolean selected = (Boolean) value;
            if (selected) {
                selectedObjects = element;
            } else {
                selectedObjects = null;
            }
        }
    }

    public T getSelectedObject() {
        return selectedObjects;
    }
}
